package interview;

import java.util.HashMap;

public class lc_k_reverse {
    static class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next;
      }
  }

    public static void main(String[] args) {

    }
    public static ListNode reverseKGroup(ListNode head, int k) {
        if(head == null)
            return null;
        ListNode pHead = new ListNode();
        pHead.next = head;
        int sum = 0;
        ListNode node = head;
        while(node != null){
            sum++;
            node = node.next;
        }
        int n = sum / k;
        if(n == 0)
            return head;
        ListNode pre = pHead;
        node = head;
        ListNode next_node = node;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < k - 1; j++) {
                next_node = node.next;
                node.next = next_node.next;
                next_node.next = pre.next;
                pre.next = next_node;
            }
            pre = node;
            node = node.next;
            next_node = node;
        }
        return pHead.next;
    }
}
